#include "HighScore.h"

// Initialize the instance to null
HighScore* HighScore::m_HighScoreInstance = 0;

HighScore::HighScore(void)
{

}

HighScore::~HighScore(void)
{
}

HighScore* HighScore::GetInstance(void)
{
	// Create the singleton if it doesn't exist
	if(!m_HighScoreInstance)
		m_HighScoreInstance = new HighScore;

	// Return the singleton
	return m_HighScoreInstance;
}

void HighScore::DeleteHighScoreInstance(void)
{
	// If instance exist, delete it!
	if(m_HighScoreInstance)
	{
		delete m_HighScoreInstance;
		m_HighScoreInstance = NULL;
	}
}

void HighScore::InitHighScore(IDirect3DDevice9* m_pD3DDevice, HWND hWnd, HINSTANCE hInst, DirectInput* directInput)
{
	// Get instances of Text
	m_pText = Text::GetInstance();
	// Create the DirectInput object
	m_pDirectInput = directInput;

	// Set title strings
	strLname = "Name";
	strLscore = "Score";
	bPass = false;
}

void HighScore::WriteToFile()
{
	// Create inFile and open the text file
	ofstream outFile("HighScore.txt");

	// If outFile has been created write these names to file
	for( short i = 0; i < sSize; ++i)
	{
		outFile << Scores[i].strScore << "," << Scores[i].strName << endl;
	}

	// Close the file
	outFile.close();

}

void HighScore::ReadFromFile()
{
	// Create inFile and open the text file
	ifstream inFile;
	inFile.open("HighScore.txt");

	// Read all lines from text file
	// and store them into arrays
	for( short i = 0; i < sSize; i++)
	{
		getline(inFile, Scores[i].strScore, ',');
		getline(inFile, Scores[i].strName);
	}

	// Close the file
	inFile.close();

}

void HighScore::SortBoard()
{
	// Convert strings to shorts
	for( int i = 0; i < sSize; i++ )
	{
		Scores[i].sScores = atoi(Scores[i].strScore.c_str());
	}

	// Compare ints and sort the board from highest to lowest
	for( int iPass = 0; iPass < (sSize - 1); iPass++)
	{
		for( int i = 0; i < (sSize -1); i++)
		{
			if( Scores[i].sScores < Scores[i +1].sScores )
			{
				// sort score
				Scores[i].sTemp = Scores[i].sScores;
				Scores[i].sScores = Scores[i+1].sScores;
				Scores[i+1].sScores = Scores[i].sTemp;
				// sort name
				Scores[i].strTmp = Scores[i].strName;
				Scores[i].strName = Scores[i+1].strName;
				Scores[i+1].strName = Scores[i].strTmp;
			}	
		}
	}

	// convert ints back to strings
	for( int i = 0; i < sSize; i++ )
	{
		stringstream out;
		out << Scores[i].sScores;
		Scores[i].strScore = out.str();
	}

}

void HighScore::ShowBoard()
{
	// Print the highscore board
	m_pText->SetName11(strLname);
	m_pText->SetScore11(strLscore);
	m_pText->SetScore1(Scores[0].strScore);
	m_pText->SetName1(Scores[0].strName);
	m_pText->SetScore2(Scores[1].strScore);
	m_pText->SetName2(Scores[1].strName);
	m_pText->SetScore3(Scores[2].strScore);
	m_pText->SetName3(Scores[2].strName);
	m_pText->SetScore4(Scores[3].strScore);
	m_pText->SetName4(Scores[3].strName);
	m_pText->SetScore5(Scores[4].strScore);
	m_pText->SetName5(Scores[4].strName);
	m_pText->SetScore6(Scores[5].strScore);
	m_pText->SetName6(Scores[5].strName);
	m_pText->SetScore7(Scores[6].strScore);
	m_pText->SetName7(Scores[6].strName);
	m_pText->SetScore8(Scores[7].strScore);
	m_pText->SetName8(Scores[7].strName);
	m_pText->SetScore9(Scores[8].strScore);
	m_pText->SetName9(Scores[8].strName);
	m_pText->SetScore10(Scores[9].strScore);
	m_pText->SetName10(Scores[9].strName);
}

bool HighScore::GetUserName(HWND hWnd)
{
	m_pDirectInput->Update(hWnd);

	// Get input from user
	if( bPass == false )
	{
		if( m_pDirectInput->CheckKeyboard(DIK_Q) )
		{
			if(!bKeyDown[DIK_Q])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'Q';
					else
						strLName += 'q';
				}
				bKeyDown[DIK_Q] = true;
			}
		}
		else
			bKeyDown[DIK_Q] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_W) )
		{
			if(!bKeyDown[DIK_W])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'W';
					else
						strLName += 'w';
				}
				bKeyDown[DIK_W] = true;
			}
		}
		else
			bKeyDown[DIK_W] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_E) )
		{
			if(!bKeyDown[DIK_E])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'E';
					else
						strLName += 'e';
				}
				bKeyDown[DIK_E] = true;
			}
		}
		else
			bKeyDown[DIK_E] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_R) )
		{
			if(!bKeyDown[DIK_R])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'R';
					else
						strLName += 'r';
				}
				bKeyDown[DIK_R] = true;
			}
		}
		else
			bKeyDown[DIK_R] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_T) )
		{
			if(!bKeyDown[DIK_T])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'T';
					else
						strLName += 't';
				}
				bKeyDown[DIK_T] = true;
			}
		}
		else
			bKeyDown[DIK_T] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_Y) )
		{
			if(!bKeyDown[DIK_Y])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'Y';
					else
						strLName += 'y';
				}
				bKeyDown[DIK_Y] = true;
			}
		}
		else
			bKeyDown[DIK_Y] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_U) )
		{
			if(!bKeyDown[DIK_U])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'U';
					else
						strLName += 'u';
				}
				bKeyDown[DIK_U] = true;
			}
		}
		else
			bKeyDown[DIK_U] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_I) )
		{
			if(!bKeyDown[DIK_I])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'I';
					else
						strLName += 'i';
				}
				bKeyDown[DIK_I] = true;
			}
		}
		else
			bKeyDown[DIK_I] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_O) )
		{
			if(!bKeyDown[DIK_O])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'O';
					else
						strLName += 'o';
				}
				bKeyDown[DIK_O] = true;
			}
		}
		else
			bKeyDown[DIK_O] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_P) )
		{
			if(!bKeyDown[DIK_P])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'P';
					else
						strLName += 'p';
				}
				bKeyDown[DIK_P] = true;
			}
		}
		else
			bKeyDown[DIK_P] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_A) )
		{
			if(!bKeyDown[DIK_A])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'A';
					else
						strLName += 'a';
				}
				bKeyDown[DIK_A] = true;
			}
		}
		else
			bKeyDown[DIK_A] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_S) )
		{
			if(!bKeyDown[DIK_S])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'S';
					else
						strLName += 's';
				}
				bKeyDown[DIK_S] = true;
			}
		}
		else
			bKeyDown[DIK_S] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_D) )
		{
			if(!bKeyDown[DIK_D])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'D';
					else
						strLName += 'd';
				}
				bKeyDown[DIK_D] = true;
			}
		}
		else
			bKeyDown[DIK_D] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_F) )
		{
			if(!bKeyDown[DIK_F])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'F';
					else
						strLName += 'f';
				}
				bKeyDown[DIK_F] = true;
			}
		}
		else
			bKeyDown[DIK_F] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_G) )
		{
			if(!bKeyDown[DIK_G])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'G';
					else
						strLName += 'g';
				}
				bKeyDown[DIK_G] = true;
			}
		}
		else
			bKeyDown[DIK_G] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_H) )
		{
			if(!bKeyDown[DIK_H])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'H';
					else
						strLName += 'h';
				}
				bKeyDown[DIK_H] = true;
			}
		}
		else
			bKeyDown[DIK_H] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_J) )
		{
			if(!bKeyDown[DIK_J])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'J';
					else
						strLName += 'j';
				}
				bKeyDown[DIK_J] = true;
			}
		}
		else
			bKeyDown[DIK_J] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_K) )
		{
			if(!bKeyDown[DIK_K])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'K';
					else
						strLName += 'k';
				}
				bKeyDown[DIK_K] = true;
			}
		}
		else
			bKeyDown[DIK_K] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_L) )
		{
			if(!bKeyDown[DIK_L])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'L';
					else
						strLName += 'l';
				}
				bKeyDown[DIK_L] = true;
			}
		}
		else
			bKeyDown[DIK_L] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_Z) )
		{
			if(!bKeyDown[DIK_Z])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'Z';
					else
						strLName += 'z';
				}
				bKeyDown[DIK_Z] = true;
			}
		}
		else
			bKeyDown[DIK_Z] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_X) )
		{
			if(!bKeyDown[DIK_X])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'X';
					else
						strLName += 'x';
				}
				bKeyDown[DIK_X] = true;
			}
		}
		else
			bKeyDown[DIK_X] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_C) )
		{
			if(!bKeyDown[DIK_C])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'C';
					else
						strLName += 'c';
				}
				bKeyDown[DIK_C] = true;
			}
		}
		else
			bKeyDown[DIK_C] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_V) )
		{
			if(!bKeyDown[DIK_V])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'V';
					else
						strLName += 'v';
				}
				bKeyDown[DIK_V] = true;
			}
		}
		else
			bKeyDown[DIK_V] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_B) )
		{
			if(!bKeyDown[DIK_B])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'B';
					else
						strLName += 'b';
				}
				(bKeyDown[DIK_B]) = true;
			}	
		}
		else
			bKeyDown[DIK_B] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_N) )
		{
			if(!bKeyDown[DIK_N])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'N';
					else
						strLName += 'n';
				}
				(bKeyDown[DIK_N]) = true;
			}
		}
		else
			bKeyDown[DIK_N] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_M) )
		{
			if(!bKeyDown[DIK_M])
			{
				if( strLName.length() <= 13 )
				{
					if( m_pDirectInput->CheckKeyboard(DIK_LSHIFT) || m_pDirectInput->CheckKeyboard(DIK_RSHIFT) )
						strLName += 'M';
					else
						strLName += 'm';
				}
				bKeyDown[DIK_M] = true;
			}
		}
		else
			bKeyDown[DIK_M] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_SPACE) )
		{
			if(!bKeyDown[DIK_SPACE])
			{
				if( strLName.length() <= 13 )
				{
					strLName += ' ';
				}
				bKeyDown[DIK_SPACE] = true;
			}
		}
		else
			bKeyDown[DIK_SPACE] = false;

		if ( m_pDirectInput->CheckKeyboard(DIK_BACKSPACE) || m_pDirectInput->CheckKeyboard(DIK_DELETE) )
		{
			if( strLName.length() > 0 )
				strLName = strLName.substr(0, strLName.length() -1 );

		}
		if ( m_pDirectInput->CheckKeyboard(DIK_RETURN) )
		{
			if( strLName.length() > 0 )
				bPass = true;
		}
		m_pText->SetHighScore(strLName);

		return false;
	}

	return true;
}

string HighScore::ReturnName(void)
{
	// Return the input name
	return strLName;
}